#include "loginproxy.h"
#include "core.h"
#include <QObject>
#include <QMessageBox>

namespace SailmiPIM {

LoginProxy::LoginProxy(QObject *parent)
    : QObject{parent}
{

}

void LoginProxy::checkLogin(){
    qDebug() << "LoginProxy::checkLogin:start check login!";
    UserInfo *userInfo = Core::Instance().GetUserManager()->userInfo;
    if(userInfo!=NULL){
        if(userInfo->isLogined){
            emit loginSuccessed(userInfo);
        }
    }
    emit loginFailed(userInfo);
}

void LoginProxy::showLogin(){
    UserInfo *userInfo = new UserInfo();
    IUserProxy *userProxy = Core::Instance().GetUserManager()->getUserProxy();
    if(userProxy){
        QDialog *loginDialog = userProxy->getLoginDialog(nullptr,userInfo);
        if(loginDialog){
            loginDialog->setModal(true);
            //loginDialog->show();
            if(loginDialog->exec()==QDialog::Accepted){
                checkLogin();
            }
        }
    }else{
        QMessageBox::information(nullptr,"SailmiPIM","No User Proxy!",QMessageBox::Ok);
        emit(loginFailed(userInfo));
    }
  }
} // namespace SailmiPIM
